<?php
//开始替换模版
define("PATH",__DIR__);
require  "function.php";

$select=require PATH."/temp/select.php";
$where=trim(file_get_contents(PATH."/temp/onlywhere.dat"));
$order=require PATH."/temp/order.php";
$join=require PATH."/temp/join.php";
$search=require PATH."/temp/search.php";
$add=require PATH."/temp/add.php";

$controller=require PATH.'/temp/Make.php';



//var_dump($search);
//var_dump($order);

$selectString="  ";
$whereString="  ";
$orderString=" ->order('content') ";
$joinString=" ";
$searchString="";  //查询的获取参数以及判空
$searchWhere=""; //查询的where数组生成
//查询的字段
foreach ($select as $key=>$item) {

      $selectString.=' '.$key.',';
}

$selectString = trim(substr($selectString,0,strlen($selectString)-1));

foreach ($join as $key=>$item) {

    $joinString.=" ->join('{$item}') \r";
}

//不变的where条件
$whereString = substr($whereString,0,strlen($whereString)-1);

$whereString.=" ->where({$where})";

$searchString=' $where=[]; '."\n";

$searchWhereString=' ->where([{@where}])';

//搜索条件
foreach ($search as $item) {

    $temp='$search_'.$item["column"]."=I('{$item['column']}');\n";
    $searchString.=$temp;

}
foreach ($search as $item) {

    $temp='!empty($search_'.$item["column"].") ?".' $where['."'{$item['column']}']".
        '=$search_'.$item["column"]. " : __DIR__ ;\n";
    $searchString.=$temp;

}
//搜索条件的 ->where() 方法
$tempSearchWhere="";
foreach ($search as $item) {

    if($item['type']=='like'){
        $temp="'{$item['column']}'"."=>['like',@name],";
        $temp=str_replace("@name",' " %{ $search_'.$item["column"].'}%"',$temp);
    }else if($item['type']=="="){
        $temp="'{$item['column']}'"."=>".'$search_'.$item["column"].",";
    }

    $tempSearchWhere.=$temp;

}
$searchWhereString=str_replace('{@where}',$tempSearchWhere,$searchWhereString);








//排序条件
$orderContent="";
foreach ($order as $key=>$item) {

    $orderContent.=' '.$key." {$item}".',';
}
$orderContent = substr($orderContent,0,strlen($orderContent)-1);

$orderString=str_replace("content","$orderContent",$orderString);

$info=file_get_contents(PATH.'/blade/controller.blade');

//开始替换(selectAction 的方法)
$info=str_replace("{{selectfield}}",$selectString,$info);
$info=str_replace("{{where}}",trim($whereString),$info);
$info=str_replace("{{order}}",$orderString,$info);

$info=str_replace("{{search}}",$searchString,$info);

$info=str_replace("{{controller}}",$controller['Controller'],$info);
$info=str_replace("{{service}}",$controller['Controller']."Service",$info);


$info=str_replace("{{model}}",$controller['table'],$info);
$info=str_replace("{{BModel}}",$controller['BModel'],$info);
$info=str_replace("{{infoModel}}",$controller['infoModel'],$info);
$info=str_replace("{{join}}",trim($joinString),$info);

$info=str_replace("{{searchWhere}}",$searchWhereString,$info);
//开始搞add的方法

$addString=' $addData=[];'." \n";

$validateString="";

//{{addcode}}

foreach ($add as $item) {
    $temp='$add_'.$item["column"]."=I('{$item['column']}');\n";
    $addString.=$temp;
}

foreach ($add as $item) {
    $temp='!empty($add_'.$item["column"].") ?".' $addData['."'{$item['column']}']".
        '=$add_'.$item["column"]. " : ".' $this->AjaxError('."'{$item["value"]}不能为空!'".") ;\n";
    $addString.=$temp;
}

foreach ($add as $item) {

    $var=explode("|",$item['validate']);

    $temp=' if(strlen($add_'.$item['column'].")<".$var[0].'){  $this->AjaxError(" {{error}} "); }'."\n";
    $error=$item['value']."不得小于{$var[0]}位!";

    $temp=str_replace("{{error}}",$error,$temp);
    $validateString.=$temp;
}


$info=str_replace("{{addcode}}",$addString."\n".$validateString,$info);

$info=str_replace("{{table_key}}",$controller['key'],$info);

//echo $whereString;
file_put_contents(PATH.'/html/'.$controller['Controller'].'Controller.class.php',$info);
//var_dump($info);


//$SelectArrString=str_replace("content","$temp",$SelectArrString);







//$zhi=$DB->query('select * from admininfo');
//var_dump($zhi);



